'use strict';


const db = uniCloud.database()
exports.main = async (event, context) => {
	await uniCloud.callFunction({
		name:"order_timeout"
	})
	
	//event为客户端上传的参数
	console.log('event : ', event)
	const arrangement = event.arrangement
	const user_plus = event.plus
	const level = event.level
	const type = event.type
	const service_location = event.service_location
	const dbCmd = db.command
	const $ = dbCmd.aggregate

	let res = await db.collection('uni-id-users')
		.aggregate()
		.match({
			'paramedic_profile.service_location': dbCmd.neq(null),
			'paramedic_profile.work_status': true,
			role: "paramedic",
			'paramedic_profile.service_id': event.service_id
		})
		.project({
			repeat: $.setIntersection([
				arrangement, '$paramedic_profile.arrangement'
			]),
			included_type: $.in([service_location, '$paramedic_profile.service_location']),
			paramedic_profile: 1,
		})
		.match({
			included_type: dbCmd.neq([]).and(dbCmd.neq(null)),
			paramedic_profile:{
				plus:dbCmd.all(user_plus)
			},
			repeat: dbCmd.eq([]).or(dbCmd.eq(null)),
		})
		.end()

	console.log(res)
	return res
};
